require('./personalCenter.less');

var _user = require("service/user-service.js");
var _util = require("util/util.js");
var _order = require("service/order-service.js");
var _user = require("service/user-service.js");

var pagination = require("../common/pagination.string")


var personalCenter = require("./personalCenter.string");

var page = {
	init: function () {
		this.loadPersonalCenter();
		this.bindEvent();
	},
	loadPersonalCenter: function () {
		_order.list({pageSize:6},function(res){
			console.log(res)
			var personalCenterHtml = _util.renderHtml(personalCenter,res);
			$("#personalCenterMain").html(personalCenterHtml);
			page.loadUsername();
			// 渲染分页
			var paginaitionHtml = _util.renderHtml(pagination);
			$("#naginationNav").html(paginaitionHtml);
			$(function () {
				var next = $("#paginationNext");
				var before = $("#paginationBefore");
				var removed = "<li class='page-item disabled' id='paginationNext'><a class='page-link' href='#'>...</a></li>"
				// 下一页
				if (!res.isLastPage) {
					next.unbind();
					next.removeClass('disabled');
					next.one("click", function () {
						page.loadArticleList(res.nextPage, res.pageSize);
						return false;
					})
				} else {
					next.addClass('disabled');
				}
				// 上一页
				if (!res.isFirstPage) {
					before.unbind();
					before.removeClass('disabled');
					before.one("click", function () {
						page.loadArticleList(res.prePage, res.pageSize);
						return false;
					})
				} else {
					before.addClass('disabled');
				}
				for (var i = 1; i < $(".page-item").length - 1; i++) {
					// 给当前页分页添加active
					if ($(".page-item").eq(i).text() == res.pageNum) {
						$(".page-item").eq(i).addClass("active");
					}
					// 给链接添加点击事件					
					$(".page-item").eq(i).one("click", function () {
						page.loadArticleList($(this).text(), res.pageSize);
						return false;
					})
				}
			})
		},function(err){
			alert(err);
		})
	},
	loadUsername : function(){
		_user.get_information(function(res){
			$(".username-h").html("您好，"+res.username+"。")
		},function(err){
			
		})
	},
	bindEvent : function(){
		// 去个人中心按钮绑定事件
		$(document).on('click','.goToMainStageBtn',function(){
			$(location).prop('href', 'http://www.westonwh.cn/personalCenter.html');
		})
		// 点开菜单栏
		$(document).on('show.bs.collapse',".collapse", function () {
			thisCollapse = $(this).prev();
			thisCollapse.find('.collapseBtn').removeClass('fa-angle-down').addClass('fa-angle-up').text(" 收起");
			thisCollapse.find('.orderDetailBtn').hide()
		})
		$(document).on('hide.bs.collapse',".collapse", function () {
			thisCollapse = $(this).prev();
			thisCollapse.find('.collapseBtn').removeClass('fa-angle-up').addClass('fa-angle-down')		.text(" 展开")	;
			thisCollapse.find('.orderDetailBtn').show()
		})
		//订单详情
		$(document).on("click",".orderDetailBtn",function(){
			window.location.href = "./orderDetail.html?orderNo="+$(this).data("order-no");
		})

	},
}

$(function () {
	page.init();
})